import { songFormat, songFormat2 } from '@renderer/utils/format'
import { defineStore } from 'pinia'

export interface IAutioType {
  id: number
  name: string
  picUrl: string
  songNmae: string
  isSong: boolean
}

const usePlay = defineStore('usePlay', {
  state: () => ({
    currentAudio: {} as IAutioType, // 当前播放的歌曲
    playMode: 'list' as 'list' | 'cycle' | 'random', // 列表播放 随记播放 单曲循环
    playList: [] as IAutioType[], // 播放列表
    playAudioIndex: 0, // 当前播放的歌曲索引
    volume: JSON.parse(localStorage.getItem('volume') || '0.5'), // 音量
    playStatus: false, // 播放状态
    avatarPlay: false, // 头像播放状态
    showLyricDrawer: false // 是否显示歌词抽屉
  }),
  actions: {
    // 初始化播放列表和正在播放歌曲
    initPlayList() {
      this.currentAudio = JSON.parse(localStorage.getItem('currentAudio') || '{}')
      this.playList = JSON.parse(localStorage.getItem('playList') || '[]')
      this.volume = JSON.parse(localStorage.getItem('volume') || '0.5')
    },
    // 设置音量
    setVolume(v: number) {
      this.volume = v
      localStorage.setItem('volume', JSON.stringify(v))
    },
    // 设置当前播放的歌曲
    setCurrentAudio(audio: any) {
      this.currentAudio = songFormat(audio)
      localStorage.setItem('currentAudio', JSON.stringify(this.currentAudio))
    },
    setCurrentAudio2(audio: any) {
      this.currentAudio = songFormat2(audio)
      localStorage.setItem('currentAudio', JSON.stringify(this.currentAudio))
    },

    // 从列表中删除某一项
    deleteAudio(id: number) {
      this.playList = this.playList.filter((item) => item.id !== id)
    },

    // 设置播放列表
    setPlayList(audio: any) {
      for (const item of this.playList) {
        item.isSong = false
      }
      const a = songFormat(audio)
      if (this.playList.length === 0) {
        a.isSong = true
        this.playList.push(a)
      } else {
        const index = this.playList.findIndex((item) => item.id === a.id)
        if (index === -1) {
          a.isSong = true
          this.playList.push(a)
        } else {
          this.playList[index].isSong = true
        }
      }

      localStorage.setItem('playList', JSON.stringify(this.playList))
    },
    setPlayList2(audio: any) {
      for (const item of this.playList) {
        item.isSong = false
      }
      const a = songFormat2(audio)
      if (this.playList.length === 0) {
        a.isSong = true
        this.playList.push(a)
      } else {
        const index = this.playList.findIndex((item) => item.id === a.id)
        if (index === -1) {
          a.isSong = true
          this.playList.push(a)
        } else {
          this.playList[index].isSong = true
        }
      }

      localStorage.setItem('playList', JSON.stringify(this.playList))
    },
    // 点击上一首
    prevAudio() {
      if (this.playAudioIndex === 0) {
        this.playAudioIndex = this.playList.length - 1
      } else {
        this.playAudioIndex--
      }
      this.currentAudio = this.playList[this.playAudioIndex]
      for (const item of this.playList) {
        item.isSong = false
      }
      this.playList[this.playAudioIndex].isSong = true
    },
    // 点击下一首
    nextAudio() {
      if (this.playAudioIndex === this.playList.length - 1) {
        this.playAudioIndex = 0
      } else {
        this.playAudioIndex++
      }
      this.currentAudio = this.playList[this.playAudioIndex]
      for (const item of this.playList) {
        item.isSong = false
      }
      this.playList[this.playAudioIndex].isSong = true
    },
    // 修改播放状态
    setPlayStatus(status: boolean) {
      this.playStatus = status
    },
    // 点击头像播放
    setAvatarPlay() {
      this.avatarPlay = !this.avatarPlay
    },
    // 设置显示歌词弹层
    setShowLyricDrawer() {
      this.showLyricDrawer = !this.showLyricDrawer
    }
  }
})
export default usePlay
